package com.dyz.swordFingerOffer;

public class StrToInt66 {
    public int StrToInt(String str) {
        if(str==null || str.length()==0){
            return 0;
        }
        //去空格
        str = str.trim();
        //只有一个字符时，如果时+或者-直接返回0
        if(str.length()==1&& (str.charAt(0)=='+' || str.charAt(0)=='-')){return 0;}
        char [] c = str.toCharArray();
        int sum = 0;
        int index = c[0]=='+' || c[0]=='-'?1:0;
        int flag = c[0]=='-'?-1:1;
        for (int i=index; i<c.length;i++){
            if(!(c[i]>=48 && c[i]<=57)){
                return 0;
            }
            sum = (sum*10+(c[i]-48));
        }

        return sum*flag;
    }

}
